<?php
require_once("model/Product.php");

abstract class AbstractPager {
	
	/**
	 * Kolik zaznamu se bude zobrazovat
	 */
	private $counter;
	/**
	 * String obsahujici prepsanou URL, napr. predchozi parametry
	 * napr. index.php?cat=6&subcat=3
	 */
	private $rewriteUrl;
	
	/**
	 * String osahujici styl pro cisla pod zaznamy
	 */
	private $pagesStyle;
	/**
	 * boolean - budou zobrazovany navigacni sipky ?
	 */
	private $arrows;
	
	private $log;
	
	public function __construct($pole,$currentPage,$celkovyPocet, $config){

		$this->log = 0;

		//default pro zobrazeni je 3
		$this->counter = $config['counter']; //domluvene
		$this->rewriteUrl = $config['rewrite'];
		$this->pagesStyle = $config['numStyle'];
		$this->arrows = $config['arrows'];
		
		
		if($this->log == 1){
			echo "==konstruktor pageru==<br>";
			echo $this->counter."<br>";
			echo $this->rewriteUrl."<br>";
			echo $this->pagesStyle."<br>";
			echo $this->arrows."<br>";
		}
		
		
//		if(($this->counter == null)||($this->counter <= 0)) {
//			
//			$this->counter = 3;
//		}
//		
//		//defaultne zapnute
//		if($this->arrows == null) {
//			echo "arrows je null<br>";
//			$this->arrows = 1;
//		}		
		
		$this->createPagerInternal($pole,$currentPage, $celkovyPocet);
	}
	
	public abstract function renderData($data, $pagor);
	
	private function createPagerInternal($data,$curPage, $celkPocet){
		
		$poKolika = count($data); //zatim napevno, jinak z configu
	
		$this->renderData($data, $poKolika);
		
		echo "<br>";
		
		$p = $celkPocet / $poKolika;
		
		$flag = 0;
		if($poKolika < $this->counter){
			$p = $celkPocet / $this->counter; // ;D
			$flag = 1;
		}
		
		
		$before = $curPage-1;
		if(($before >= 0) && ($this->arrows == 1)){
			echo "<span style='".$this->pagesStyle."'><a href='".$this->rewriteUrl."&page=".$before."'>&lt;</a></span>";
			echo "&nbsp;";
		}
		
		for($j = 0, $s = 1; $s<$p;$j++,$s++){ //$j<$p$
			if($curPage == $j) {//??
					echo "<span style='".$this->pagesStyle."'>".$s."</span>";
					echo "&nbsp;";
				}
				else {				
					//index.php?cat=&subcat=
					echo "<span style='".$this->pagesStyle."'><a href='".$this->rewriteUrl."&page=".$j."'>".$s."</a></span>";
					//index.php?cat=".$categorie."&subcat=".$subkategorie."
					echo "&nbsp;";
				}
			}
		
	//	$zbytek = $celkPocet % $poKolika;
		
		$zbytek1 = $celkPocet % $this->counter;
		
		if(($zbytek1 >= 0)) { 
			
			if($curPage == $j) {//s
					echo "<span style='".$this->pagesStyle."'>".$s."</span>";
					echo "&nbsp;";
				}
				else {
					echo "<span style='".$this->pagesStyle."'><a href='".$this->rewriteUrl."&page=".$j."'>".$s."</a></span>";
					echo "&nbsp;";
				}
	
			$p++;
		}
		
		$next = $curPage+1;
		if(($next < ($p-1)) && ($this->arrows == 1)){
			echo "<span style='".$this->pagesStyle."'><a href='".$this->rewriteUrl."&page=".$next."'>&gt;</a></span>";
		}
		
		if ($this->log == 1) {
		echo "<br>Aktualni stranka / Celkem stranek ".$curPage." / ".$p;
		echo "<br>Pocet zobrazenych zaznamu / Pocet celkem ".$poKolika." / ".$celkPocet;
		echo "<br>".$celkPocet." % ".$poKolika." = ".$zbytek1;
		}
	}
	
}
?>
